package com.bingo.bounce.sdk.utils;

import com.bingo.bounce.sdk.ad.BBounceMaxReward;
import com.bingo.bounce.sdk.ad.BBounceVideoIAP;
import com.bingo.bounce.sdk.ad.BBounceVideoManger;
import com.bingo.bounce.sdk.ad.BBounceVideoRewardCallback;
import com.bingo.bounce.sdk.bridge.BBounceCocosBridge;

import org.json.JSONObject;

public class BBounceADUtils {

    public static void playAd(String adSource) {
        try {
            BBounceVideoIAP adService = null;
            if (BBounceMaxReward.getInstance().isReady()) {
                adService = BBounceVideoManger.getReward1Instance();

            }

            if (adService == null) {

                callbackAdError();
                return;
            }

            callback(adService,adSource);


        } catch (Exception e) {
            e.printStackTrace();
            callbackAdError();
        }
    }

    private static void callback(BBounceVideoIAP adService, String adSource) {
        adService.showAd(BBounceCocosBridge.mainGameActivity.get(), new BBounceVideoRewardCallback() {
            @Override
            public void onAdOver() {

                callbackAdOver(adSource);
            }

            @Override
            public void onAdError() {
                callbackAdError();

            }
        });

    }

    private static void callbackAdOver(String adSource) {
        try {
            JSONObject jsonObject = getJson(adSource);
            BBounceCocosBridge.getInstance().XSSdkCallback("ad_over_wh_a", jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void callbackAdError() {
        try {
            BBounceCocosBridge.getInstance().XSSdkCallback("ad_error_wh_a", "{}");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static JSONObject getJson(String adSource) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ad_source", adSource);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return jsonObject;
    }

}
